package com.weiqk.pmdemo.service.impl;

import com.weiqk.pmdemo.dao.ApplyLinkRepository;
import com.weiqk.pmdemo.dao.ProjectRepository;
import com.weiqk.pmdemo.dto.ApplyLinkDto;
import com.weiqk.pmdemo.dto.ApplyLinkInDto;
import com.weiqk.pmdemo.pojo.ApplyLink;
import com.weiqk.pmdemo.pojo.Project;
import com.weiqk.pmdemo.service.ApplyLinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ApplyLinkServiceImpl implements ApplyLinkService {
    @Autowired
    ApplyLinkRepository applyLinkRepository;
    @Autowired
    DicServiceImpl dicService;
    @Autowired
    ProjectRepository projectRepository;
    @Override
    public List<ApplyLinkDto> findAll() {
        Iterable<ApplyLink> applyLink = applyLinkRepository.findAll();
        List<ApplyLinkDto> applyLinkDto= new ArrayList<>();
        for (ApplyLink a:applyLink){
            int materialCode = a.getMaterialCode();
            String materialName = dicService.findMaterialNameByDicCode(materialCode);
            ApplyLinkDto ald= new ApplyLinkDto();
            ald.setApplyLink(a);
            ald.setMaterialName(materialName);
            applyLinkDto.add(ald);
        }
        return applyLinkDto;
    }

    @Override
    public List<ApplyLinkDto> findfalgnull() {
        List<ApplyLink> applyLink = applyLinkRepository.findfalgnull();
        List<ApplyLinkDto> applyLinkDto= new ArrayList<>();
        for (ApplyLink a:applyLink){
            int materialCode = a.getMaterialCode();
            String materialName = dicService.findMaterialNameByDicCode(materialCode);
            ApplyLinkDto ald= new ApplyLinkDto();
            ald.setApplyLink(a);
            ald.setMaterialName(materialName);
            applyLinkDto.add(ald);
        }
        return applyLinkDto;
    }

    @Override
    public Boolean deleteById(int id) {
        applyLinkRepository.deleteById(id);
//        if (applyLinkRepository.findById(id).isEmpty()){
//            return false;
//        }
//        return true;
        return true;
    }

    @Override
    public Boolean update(ApplyLinkInDto applyLinkInDto) {
        //处理时间
        ApplyLink applyLink = applyLinkInDto.getApplyLink();
        applyLink.setUpadteTime(new Date());
        //处理外键数据
        int projectNum = applyLinkInDto.getProjectNum();
        Project project = projectRepository.findOneById(projectNum);
        applyLink.setProject(project);
        //操作数据库
        applyLinkRepository.save(applyLink);
        return true;
    }

    @Override
    public Boolean put(ApplyLinkInDto applyLinkInDto) {
        //处理时间
        ApplyLink applyLink = applyLinkInDto.getApplyLink();
        applyLink.setCreateTime(new Date());
        applyLink.setUpadteTime(new Date());
        //处理外键数据
        int projectNum = applyLinkInDto.getProjectNum();
        Project project = projectRepository.findOneById(projectNum);
        applyLink.setProject(project);

        //操作数据库
        applyLinkRepository.save(applyLink);
        return true;
    }
}
